package eng.agile;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class TrakerActivity extends MapActivity implements LocationListener {
	static String value1;
	static String value2;
	static EditText e1;
	static EditText e2;
	static TextView curText;
	MapView m;
	int longit = 0;
	int latit = 0;
	LocationManager locMan;
	String towers;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findViewById(R.id.button3).setVisibility(4);
		m = (MapView) findViewById(R.id.mapView);
		m.setBuiltInZoomControls(true);
		// m.setSatellite(true);
		// m.setStreetView(true);
		final Builder builder = new AlertDialog.Builder(this);
		final Button route1 = (Button) findViewById(R.id.button2);
		

		route1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				findViewById(R.id.button3).setVisibility(4);
				e1 = (EditText) findViewById(R.id.editText1);
				e2 = (EditText) findViewById(R.id.editText2);
				value1 = e1.getText().toString();
				value2 = e2.getText().toString();
				if (e2.getText().length() > 0) {
					if (e1.getText().length() == 0) {

						// e1.setText("Current Location");

					}
					builder.setMessage(
							"Route From : " + e1.getText().toString()
									+ "\nto : " + e2.getText().toString())
							.setCancelable(true)
							.setPositiveButton("yes",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											// put calling function to get route
											// from maps

											findViewById(R.id.button3)
													.setVisibility(1);
											findViewById(R.id.linearLayoutMain)
													.setVisibility(4);
											findViewById(R.id.progressBar1)
													.setVisibility(1);
										}
									})
							.setNegativeButton("No",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											dialog.cancel();
											findViewById(R.id.progressBar1)
													.setVisibility(4);
										}
									});

					AlertDialog dialog = builder.create();
					dialog.show();
				} else {
					builder.setMessage("Try to provide Destination Address")
							.setNegativeButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											dialog.cancel();
											findViewById(R.id.progressBar1)
													.setVisibility(4);
										}
									});
					AlertDialog dialog = builder.create();
					dialog.show();
				}
			}
		});

		final Button interChange = (Button) findViewById(R.id.button1);
		interChange.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				e1 = (EditText) findViewById(R.id.editText1);
				e2 = (EditText) findViewById(R.id.editText2);
				findViewById(R.id.progressBar1).setVisibility(4);
				if (e2.getText().length() > 0) {

					if (e1.getText().length() == 0) {
						// e1.setText("Current Location");

					}
					value1 = e1.getText().toString();
					value2 = e2.getText().toString();

					e1.setText(value2);
					e2.setText(value1);
				} else {
					builder.setMessage("Try to provide Destination Address")
							.setNegativeButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											dialog.cancel();
										}
									});
					AlertDialog dialog = builder.create();
					dialog.show();
				}
			}
		});
		final Button location = (Button) findViewById(R.id.button3);
		location.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				findViewById(R.id.linearLayoutMain).setVisibility(1);
				findViewById(R.id.progressBar1).setVisibility(4);
				findViewById(R.id.button3).setVisibility(4);

			}
		});
		final Button curPosition = (Button) findViewById(R.id.button4);
		curPosition.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				findViewById(R.id.linearLayoutMain).setVisibility(1);
				findViewById(R.id.progressBar1).setVisibility(4);
				findViewById(R.id.button3).setVisibility(4);
				//findViewById(R.id.button4).setVisibility(4);
				curLocation();
			}
		});

	}

	protected boolean isRouteDisplayed() {
		return false;
	}

	public void curLocation() {
		MapController mc;
		
		locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Criteria crit = new Criteria();
		towers = locMan.getBestProvider(crit, false);
		Location location = locMan.getLastKnownLocation(towers);
		
		if (location != null){
			latit = (int) (location.getLatitude()*1E6);
			longit = (int) (location.getLongitude()*1E6);
			GeoPoint ourLoc = new GeoPoint(latit, longit);
		}
		else {
			Toast.makeText(TrakerActivity.this, "Error", Toast.LENGTH_SHORT);
		}
		
		MapView mapView = (MapView) findViewById(R.id.mapView);
		final MyLocationOverlay myLocation = new MyLocationOverlay(this,
				mapView);
		mapView.getOverlays().add(myLocation);
		myLocation.enableMyLocation();

		mc = mapView.getController();
		//String coordinates[] = { "1.352566007", "103.78921587" };
		//double lat = Double.parseDouble(coordinates[0]);
		//double lng = Double.parseDouble(coordinates[1]);
		GeoPoint p = new GeoPoint((int) (latit * 1E6), (int) (longit * 1E6));
		mc.animateTo(p);
		mc.setZoom(15);
		mapView.invalidate();
	}

	public void onLocationChanged(Location l) {
		// TODO Auto-generated method stub
		//compass.disableCompass();
		latit = (int) (l.getLatitude()*1E6);
		longit = (int) (l.getLongitude()*1E6);
		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
}